#!/bin/bash

OLD_LANG=${LANG}
export LANG=en_US.UTF-8

OLD_IFS="${IFS}"
export IFS=" "


basename_script=$(basename $0)
dirname_script=$(cd $(dirname $0); pwd)
ret_main=0

set -e

function Usage {
    
	echo "bash ${basename_script}"
	echo "bash ${basename_script} --path {path_software}"
	
	
}

# 用于记录日志到日志文件。
function Log {
    
	local msg="$1"
	local level="$2"
	local name="$3"
	local ret_code=0
	
    [ -z "${msg}" ] && return 1
    [ -z "${level}" ] && level=INFO
    [ -z "${name}" ] && name=${basename_script}

    case ${level} in
    'DEBUG')
        local txt_level=$"DEBUG     "
        ;;
    'ERROR')
        local txt_level=$"ERROR     "
        ;;
    'WARN')
        local txt_level=$"WARN      "
        ;;
    'INFO')
        local txt_level=$"INFO      "
        ;;
    esac

    echo "${txt_level}: [${name}] ${msg}"
	
    return 0

}

function recover_system_param {
    export LANG=${OLD_LANG}
	export IFS="${OLD_IFS}"
}

function install_ansible{
    local path="$1"
	local ret_code=0
	
	local ansible_depend_Software=( \
	python2-pyasn1-0.1.9-7.el7.noarch.rpm \
	python-ipaddress-1.0.16-2.el7.noarch.rpm \
	python-six-1.9.0-2.el7.noarch.rpm \
	python-httplib2-0.9.2-1.el7.noarch.rpm \
	python-enum34-1.0.4-1.el7.noarch.rpm \
	libyaml-0.1.4-11.el7_0.x86_64.rpm \
	PyYAML-3.10-11.el7.x86_64.rpm \
	libtommath-0.42.0-6.el7.x86_64.rpm \
	libtomcrypt-1.17-26.el7.x86_64.rpm \
	python2-crypto-2.6.1-15.el7.x86_64.rpm \
	python-keyczar-0.71c-2.el7.noarch.rpm \
	python-backports-1.0-8.el7.x86_64.rpm \
	python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarch.rpm \
	python-setuptools-0.9.8-7.el7.noarch.rpm \
	python-babel-0.9.6-8.el7.noarch.rpm \
	python-ply-3.4-11.el7.noarch.rpm \
	python-pycparser-2.14-1.el7.noarch.rpm \
	python-cffi-1.6.0-5.el7.x86_64.rpm \
	python-markupsafe-0.11-10.el7.x86_64.rpm \
	python-jinja2-2.7.2-2.el7.noarch.rpm \
	python-idna-2.4-1.el7.noarch.rpm \
	python2-cryptography-1.7.2-2.el7.x86_64.rpm \
	python-paramiko-2.1.1-4.el7.noarch.rpm \
	sshpass-1.06-2.el7.x86_64.rpm \
	python2-jmespath-0.9.0-3.el7.noarch.rpm \
	ansible-2.6.5-1.el7.noarch.rpm )
	
    for depend_software in ${ansible_depend_Software}
	do
	    if [ ! -f ${path}/${depend_software} ]; then
		    Log "The rpm file ${depend_software} depended by ansible-2.6.5 is not exsit in path ${path}." ERROR 
			ret_code=1
			break
		fi
		rpm -U ${path}/${depend_software} > /dev/null
	done
	
	return ${ret_code}
}

function main {
    
	local ret_code=0
	
	
	if [ "$#" -lt 1 ];then
	    Log "The input param is too little, please input the right parameter." ERROR 
	fi
	
	while [ "$#" -gt 1 ]
	do
	    tag="$1"
	    case ${tag} in
	    --path | -p)
	        path_software="$2"
		    shift
	        ;;
	    *)
		    ;;
	    esac
	    shift
	done
	
	Log "Start to install ansible-2.6.5 and depend software..." INFO
	# 执行安装ansible函数
	if [ -z ${path_software} ]; then
		if [ -d ${dirname_script}/ansible ]; then
		    path_software=${dirname_script}/ansible
			Log "The path of ansible software is not input, will use the default path ${dirname_script}/ansible." INFO
        else
		    path_software=${dirname_script}
			Log "The path of ansible software is not input and the default path ${dirname_script}/ansible is not exsit. use path ${dirname_script} instead." WARN
		fi
	fi
	
	if [ ! -d ${path_software} ]; then
	    Log "The path of ansible software is not a directory, please make sure input a directory." ERROR
		Usage
		ret_code=1
		return ${ret_code}
	fi
	
	#install_ansible ${path_software}
	#if [ "$?" -ne 0 ]; then
	#    Log "install ansible-2.6.5 failed!" ERROR
	#	ret_code=1
	#	return ${ret_code}
	#fi
	Log "Success to install ansible-2.6.5 and depend software." INFO
	
	return ${ret_code}
}
Log "Start to execute script ${dirname_script}/${basename_script}..." INFO
main 
ret_main="$?"
#recover_system_param
if [ ${ret_main} -ne 0 ]; then
    Log ERROR "The script ${dirname_script}/${basename_script} execute failed."
	logger -i -t ${basename_script} "The script ${dirname_script}/${basename_script} execute failed."
	exit ${ret_main}
fi
Log "Success to execute script ${dirname_script}/${basename_script}." INFO
exit ${ret_main}


